<?php

include 'def.php';

$table = "teacher";

$json_array = array("status"=>"error","error_id"=>"post_not_set","error_description"=>"no request data received");

if( empty($_GET) )
{	/*do nothing*/
	$json_array["error_description"] = "empty post";
}
elseif( !isset($_GET["username"]) || !isset($_GET["password"]))
{/*do nothing*/
	$json_array["error_description"] = flatten($_GET);
}
elseif( empty($_GET["username"]) || empty($_GET["password"]) )
{/* do nothing */
	$json_array["error_description"] = "Some post values are empty";
}
else{
	
	$username = $_GET["username"];

	$password = $_GET["password"];
		
	
	$conn = mysql_connect(DBSERV,DBUSER,DBPASSWD) or die("Couldn't connect to database");

	$db = mysql_select_db(DB) or die("Couldn't connect to database");
		
	$query = "select firstname, lastname, teacherID from $table where email='$username' and password='$password';";

	$result = mysql_query($query) or die("Query_failed: " . mysql_error());

	$num_of_rows = mysql_num_rows($result);
		
		/*pick only the first row, as there should be only one row*/
	if($num_of_rows != 1)
	{/* somthing is wrong */
			$json_array = array('status'=>'error','error_id'=>'userpassword','error_description'=>'username or password not found');
	}
	else{
			
		$row = mysql_fetch_array($result);
				
		$firstname = $row["firstname"];
			
		$lastname = $row["lastname"];
			
		$teacherID = $row['teacherID'];
			
		/*set the session id values, token needs to be clearified*/
		/*session_name('Token'); set this at session directive*/
			
		session_start();
		$session_token = session_id();
			
		$_SESSION['id'] = $teacherID;
	
	    /*create json representation*/
		$json_array = array('status'=>'OK', 'session_token'=>"$session_token", 'id'=>"$teacherID");
		}
		mysql_close($conn);
}
echo json_encode($json_array) ;

?>